package com.ts.file;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;

public class FIleUpload {

    public static void main(String[] args) {

        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.region2());

        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本

        UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证，然后准备上传
        String accessKey = "1SvCrvXD0Ri6bibbnCOjVaiD5Aq84vmxGV8M-1Hj";
        String secretKey = "lDt4V2_ymbPFMUJr7yppY-lUesmTpq5cs-Xo2BMH";

        // 保存的空间
        String bucket = "2207-ts";

        // 上传的文件地址
        String localFilePath = "C:\\Users\\dashixin\\Desktop\\2207-ts-content.sql";

        String key = "weixin/2207-ts-content.sql";

        // 通过AK和SK这个一个Token
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);
        System.out.println("token:" + upToken);
        try {
            // 上传文件
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }
        }

    }
}
